<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <scroll class="content">
    <home-swiper :banners="banners"></home-swiper>
    <recommend-view :recommends="recommends"></recommend-view>
    <feature-view></feature-view>
    <tab-control :titles="['流行','新款','精选']" class="tab-control" @tabClick="tabClick"></tab-control>
    <goods-list :goods="showGoods" ></goods-list>
    </scroll>

  </div>
</template>

<script>
  //函数
  import {getHomeMultidata,getHomeGoods} from "network/home";

  //公共组件
  import navBar from "components/common/navbar/navBar";
  import TabControl from "components/content/TabControl";
  import GoodsList from "components/content/goods/GoodsList";
  import Scroll from "components/common/scroll/Scroll";

  //子组件
  import HomeSwiper from "views/home/childComps/HomeSwiper";
  import RecommendView from "views/home/childComps/RecommendView";
  import FeatureView from "views/home/childComps/FeatureView";


  export default {
    name: "Home",
    components: {
      navBar,
      TabControl,
      GoodsList,
      Scroll,

      HomeSwiper,
      RecommendView,
      FeatureView,
    },
    data() {
      return {
        banners:[],
        recommends:[],
        goods:{
          'pop': {page:0,list:[]},
          'new': {page:0,list:[]},
          'sell': {page:0,list:[]}
        },
        currencyType: 'pop',
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currencyType].list
      }
    },
    methods: {
      //事件函数
      tabClick(index) {
        console.log("-----"+index);
        switch(index){
          case 0:
            this.currencyType = 'pop';
            break;
          case 1:
            this.currencyType  = 'new';
            break;
          case 2:
            this.currencyType  = 'sell';
            break;
        }
      },

      /**
       *网络请求的方法
       */
      //获取首页多个数据
      getHomeMultidata(){
        getHomeMultidata().then(res => {
          // console.log(res);
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        });
      },
      //获取商品
      getHomeGoods(type) {
        const page = this.goods[type].page +1 ;
        getHomeGoods(type, page).then(res => {
          console.log(res)
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1;
        });
      }
    },

    created() {
      this.getHomeMultidata();
      this.getHomeGoods("pop");
      this.getHomeGoods("new");
      this.getHomeGoods("sell");

    },
  }
</script>

<style scoped>
  /*#home {*/
  /*  height: 100vh;*/
  /*  position: relative;*/
  /*}*/
  .home-nav{
    background-color: var(--color-tint);
    color:#ffffff;
    /*position: fixed;*/
    /*z-index: 9;*/
    /*top:0;*/
    /*left: 0;*/
    /*right: 0;*/
  }
  .tab-control {
    position: sticky;
    top:44px;
    z-index: 9;
  }

  .content {
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  /*.content {*/
  /*height: calc(100% - 93px);*/
  /*overflow: hidden;*/
  /*margin-top: 44px;*/
  /*}*/
</style>
